﻿using System;
using System.IO;
using System.Text;


using ns_ServerOperation;
using ns_User;
using ns_SystemConst;
using ns_FriendsList;
using ns_MsgRecords;

namespace ns_FilesManage
{
    public static class s_c_FilesManage
    {
        public static i_FilesManage FilesManage
        {
            get { return _FilesManage; }
        }
        private static c_FilesManage _FilesManage = new c_FilesManage();
    }

    public interface i_FilesManage
    {
        String NewFriendsListXml(String name); //新建FriendsList文件，返回文件路径，失败返回null
        String NewMsgGotRecordsXml(String name);//新建MsgGotRecords文件，返回文件路径，失败返回null
        String NewMsgSentRecordsXml(String name);//新建MsgSentRecords文件，返回文件路径，失败返回null
        String NewMsgNotSentRecordsXml(String name);//新建MsgNotSentRecords文件，返回文件路径，失败返回null
        String NewImgHeadPath(String name); //新建头像路径，返回默认头像文件路径，失败返回null
        String UpdateImgHeadPath(String name,String imgFileName); //新建头像路径，返回头像文件路径，失败返回null
        String ImgHeadUrl(String name);//返回头像URL
        String ImgDefaultHeadUrl(String name);//返回默认头像
        Boolean OverWriteFile(String fileName, Byte[] Content); //以content内容更替文件内容
        Boolean OverWriteFile(String fileName, String Content);//同上
    
    }
    public class c_FilesManage : i_FilesManage
    {
        public String NewFriendsListXml(String name)
        {
            FileStream fs = null;
            try
            {
                String fileName = c_Const.WEBSITE_PATH + "\\FilesSystem\\Xml\\FriendsList\\" + name + "_FriendsList.xml";
                
                c_FriendsListXml flx = new c_FriendsListXml();
                flx.SaveFriendsListXml(fileName);

                return fileName;
            }
            catch (Exception e)
            {
                if (fs != null) fs.Close();
                return null;
            }
        }
        public String NewMsgGotRecordsXml(String name)
        {
            FileStream fs =null;
            try
            {
                String fileName = c_Const.WEBSITE_PATH + "\\FilesSystem\\Xml\\MsgGotRecords\\" + name + "_MsgGotRecords.xml";

                c_MsgRecordsXml mrx = new c_MsgRecordsXml();
                mrx.SaveMsgRecordsXml(fileName);
               
                return fileName;
            }
            catch (Exception e)
            {
                if (fs != null) fs.Close();
                return null;
            }
        }
        public String NewMsgSentRecordsXml(String name)
        {
            FileStream fs = null ;
            try
            {
                String fileName = c_Const.WEBSITE_PATH + "\\FilesSystem\\Xml\\MsgSentRecords\\" + name + "_MsgSentRecords.xml";

                c_MsgRecordsXml mrx = new c_MsgRecordsXml();
                mrx.SaveMsgRecordsXml(fileName);

                return fileName;
            }
            catch (Exception e)
            {
                if (fs != null) fs.Close();
                return null;
            }
        }
        public String NewMsgNotSentRecordsXml(String name)
        {
            FileStream fs = null ;
            try
            {
                String fileName = c_Const.WEBSITE_PATH + "\\FilesSystem\\Xml\\MsgNotSentRecords\\" + name + "_MsgNotSentRecords.xml";
                c_MsgRecordsXml mrx = new c_MsgRecordsXml();
                mrx.SaveMsgRecordsXml(fileName);
                return fileName;
            }
            catch (Exception e)
            {
                if (fs != null) fs.Close();
                return null;
            }
        }
        public String NewImgHeadPath(String name)
        {
            FileStream fs = null ;
            try
            {
                String fileName = c_Const.WEBSITE_PATH + "\\Resource\\Image\\UserHead\\DefaultHead.gif";
                if (!File.Exists(fileName))
                {
                    fs= File.Open(fileName, FileMode.Create);
                    fs.Close();
                }
                return fileName;
            }
            catch (Exception e)
            {
                if (fs != null) fs.Close();
                return null;
            }
        }
        public String UpdateImgHeadPath(String name, String newImgFileName)
        {
            FileStream fs = null ;
            try
            {
                String fileName = c_Const.WEBSITE_PATH + "\\Resource\\Image\\UserHead\\" + name + "_Head.gif";
                if (!File.Exists(fileName))
                {
                    fs= File.Open(fileName, FileMode.Create);
                    fs.Close();
                }
                File.Copy(newImgFileName, fileName, true);
                return fileName;
            }
            catch (Exception e)
            {
                if (fs != null) fs.Close();
                return null;
            }
        
        }
        public String ImgHeadUrl(String name)
        {
            String fileName = c_Const.WEBSITE_PATH + "\\Resource\\Image\\UserHead\\" + name + "_Head.gif";
            String url = c_Const.WEBSITE+ "/Resource/Image/UserHead/" + name + "_Head.gif";
            if (File.Exists(fileName)) return url;
            else return null;
        }
        public String ImgDefaultHeadUrl(String name)
       {
           String fileName = c_Const.WEBSITE_PATH + "\\Resource\\Image\\UserHead\\DefaultHead.gif";
           String url = c_Const.WEBSITE + "/Resource/Image/UserHead/" + name + "_Head.gif";
           if (File.Exists(fileName)) return url;
           else return null;
       }
        public Boolean OverWriteFile(String fileName, Byte[] Content)
       {
           FileStream fs = null;
           try
           {
               if (File.Exists(fileName)) File.Delete(fileName);
                fs= File.Open(fileName, FileMode.Create);
               fs.Write(Content, 0, Content.Length);
               fs.Close();
               return true;
           }
           catch (Exception e)
           {
               if (fs != null) fs.Close();
               return false;
           }
       }
        public Boolean OverWriteFile(String fileName, String Content)
       {
           FileStream fs = null;
           try
           {
               if (File.Exists(fileName)) File.Delete(fileName);
               fs = File.Open(fileName, FileMode.Create);
               fs.Write(new UTF8Encoding(true).GetBytes(Content), 0, Content.Length);
               fs.Close();
               return true;
           }
           catch (Exception e)
           {
               if (fs != null) fs.Close();
               return false;
           }
       }
    }

}